/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery","formServ","ajaxServ","msgServ","treegridServ","sysmanMain",
	"bizProto","btnServ","deptUserSelect"], 
	function($,formServ,ajaxServ,msgServ,treegridServ,main,
		bizProto,btnServ,deptUserSelect) {

	'use strict';

	var _bizProto,$dg,$grid, $edit_window,$form,window_btn,$flowname,$parentCombo,_buttonConfig,_moduleConfig,
		
	    flowUrl = '/flowDefinition/getLastVersionAllList.action',permissionFlowUrl
    var private_method = {
    	//初始化组件对象
    	init:function(doc){
    		if (window.console) {
    		console.info('permission modules gets ready and  are loaded.');
    		}
    		_moduleConfig = main.getCurrentModuConfig();
			_bizProto = new bizProto({doc:doc,rootId:'panel-permission',panelSelector:'div.well',subSystemCode:'sysman',moduleCode:'permission'})
			_buttonConfig={
						"新增":private_method.add,
					    "编辑":private_method.edit,
						 "删除":private_method.del,
						 "保存":private_method.submit,
						 "取消":private_method.cancel,
						 "刷新缓存":private_method.refresh,
						 "标准按钮":function(e){private_method.generateBtn({type:'1',evt:e})},
						 "流程按钮":function(e){private_method.generateBtn({type:'3',evt:e})},
						 "审核按钮":function(e){private_method.generateBtn({type:'2',evt:e})},
						 "生成页面":private_method.generatePgae,
						 "生成":private_method.generate
					}
			private_method.initPath();
			
			private_method.initElement(_bizProto.pageDoc);
			
    	},
    	
    	//设置路径
    	initPath:function(){    	
    		  		
    		permissionFlowUrl = _bizProto.rootPath + flowUrl;
    	},
    	
    	//定位目标元素
    	initElement:function(doc){
    		$dg = $(doc).find("#permission-dg");
    		$grid=$dg.treegrid({				
				onDblClickRow:function(row){private_method.editExecute(false)},
				url:_bizProto.treeUrl				
			});			
			
			//编辑窗口初始化
			$edit_window = $('#permission-win')
			
			$form = $($edit_window).find('form')
			
			$parentCombo=$($edit_window).find("#parentid")
			$($parentCombo).combotree({
				width:171,
				url:_bizProto.treeUrl,
				idFiled:'id',
			 	textFiled:'name',
			 	parentField:'parentid',
			 	editable:true,
			 	onClick:function(node){private_method.setTreeValue(node,$($form).find('#parentName'))}
			 	
			});
			
			
			$flowname = $($edit_window).find("#flowid")
			$($flowname).combogrid({
				width:171,				
				url:permissionFlowUrl,
				onClickRow:function(node){private_method.setGridValue(node,$($form).find('#flowName'))}
			});
			
			
			
			//初始化操作按钮
			var listTd = $(_bizProto.pageDoc).find('#tb-permission td.list-btn')
			
			// 编辑窗口初始化
			
			var editTd = $($edit_window).find('#tb-permissionEdit div')
			
			var _param = {
				bizcode:_moduleConfig.fullBizcode,
				rootPath:_bizProto.rootPath,
				containers:[listTd,editTd],
				callback:private_method.bindBtnEvent}
			btnServ.generateListAndEditBtn(_param);
			
			var window_id = 'permission-win';
			var el = $('#'+window_id+' #leader_name');
			
			el.textbox({
				onClickButton:function(data){								
					deptUserSelect.openPage({
						userids:window_id+' #leader_ids',
						usernames:window_id+' #leader_name'					
					})								
				}
			})
			$.messager.progress('close');
    	},
    	
    	//绑定按钮事件
		bindBtnEvent:function(){
			// 绑定按钮事件
			var btn = $(_bizProto.pageDoc).find('#tb-permission a.function-btn')
			$(btn).unbind('click');
			$(btn).bind('click', private_method.function_btn_listenner)			
			
			window_btn = $($edit_window).find('a.function-btn')
			$(window_btn).unbind('click')
			$(window_btn).bind('click',	private_method.function_btn_listenner)
			
		},
    	setGridValue:function(node,target){
    		if(node!=null){
    			var _node=$($flowname).combogrid('grid').datagrid('getSelected');
    			$(target).val(_node.name);
    		}
    	},
    	setTreeValue:function(node,target){
    		if(node!=null){    			
    			$(target).val(node.name);
    		}
    	}, 
		
		//功能按钮监听函数
		function_btn_listenner:function(e){
			
			btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})
			
		},
		//新增函数
		add:function(e){
			$form = $($edit_window).find('form')
			$($form).form('clear');
			$edit_window.window('open');
			if(e && e.currentTarget){
				$(e.currentTarget).linkbutton('enable');
			}
		},
		//编辑,flag 为form按钮可见性标志
		edit:function(e){
			private_method.editExecute(true);
			if(e && e.currentTarget){
				$(e.currentTarget).linkbutton('enable');
			}
		},
		
		editExecute:function(flag){
			var _param = {flag:flag,btn:window_btn,grid:$grid,window:$edit_window,nullField:'parentid'}
			treegridServ.edit(_param);
		},
		//生成页面
		generatePgae:function(e){
			private_method.generate(e);
		}
		,
		generate:function(e){
			var _form = $('#generate-win .main-form')
			var ret = formServ.getDataObj({
				rootId:"#generate-win",
				form : _form
			});
			$.ajax({
				url	: '/'+main.getRootPath() + '/generate/generateAllPage.action',
				data: ret,
				success:function(data){
					msgServ.Msgslide("页面生成成功");
					if(e && e.currentTarget){
						$(e.currentTarget).linkbutton('enable')
					}
				}
				,
				error:function(){
					msgServ.Msgslide("页面生成失败");
					if(e && e.currentTarget){
						$(e.currentTarget).linkbutton('enable')
					}
				}
			})
			
		}
		,
		//删除
		del:function(e){
			var selectedRecord = $grid.treegrid('getSelected');
			
			var _param = {url:_bizProto.delUrl,grid:$grid,evt:e}
			treegridServ.delRow(_param);
		},
		//提交form
		submit:function(e){
			
			var _param = {url:_bizProto.saveUrl,grid:$grid,form:$form}
			treegridServ.save(_param,e)
		},
		//
		cancel:function(e){
			if(e && e.currentTarget){
				$(e.currentTarget).linkbutton('enable');
			}
			$($edit_window).window('close');
		}
		,
		
		refresh:function(e){
			var _url = '/permission/query/reloadCache.action';
			
			$.ajax({
				url: _bizProto.rootPath +_url,
				success:function(data){
					
					var _msg
					if(data && data.status && data.status==='success'){
						_msg = "缓存刷新成功!";
					}else{
						_msg = "缓存刷新失败!";
					}
					
					msgServ.Msgslide(_msg);
					if(e && e.currentTarget){
						$(e.currentTarget).linkbutton('enable');
					}
				},
				failure:function(result){
					msgServ.Msgslide('缓存刷新失败!');
					if(e && e.currentTarget){
						$(e.currentTarget).linkbutton('enable');
					}
				}
			})
			
			
		}
		,
		/**
		 * @param _options.type	按钮类型(1:标准按钮,2:审核按钮,3:流程按钮)
		 */
		generateBtn:function(_options){
			var record = $grid.treegrid('getSelected')
			if(record && record.id){
				$.ajax({
					url:_bizProto.rootPath + '/permission/generatePermissions.action',
					data:{id:record.id,type:_options.type},
					success:function(data){
						msgServ.Msgslide('操作成功');
						if(_options.evt && _options.evt.currentTarget){
							$(_options.evt.currentTarget).linkbutton('enable');
						}
					}
					,
					error:function(){
						msgServ.Msgslide('操作失败');
						if(_options.evt && _options.evt.currentTarget){
							$(_options.evt.currentTarget).linkbutton('enable');
						}
					}
				})
			}
		}
		
	}
	
	var public_method={
		//销毁方法，释放绑定事件及变量定义
		destory:function(){
			//取消编辑窗口的form按钮绑定
			
			$(window_btn).unbind('click',function(e){private_method.function_btn_listenner(e)})
			
			//取消列表窗口的按钮绑定
			var btn = $(_bizProto.pageDoc).find('a.function-btn')
			$(btn).unbind('click',function(e){private_method.function_btn_listenner(e)})
				
		},
		//初始化页面
		init:function(doc){
			private_method.init(doc)
		}
		
	}
		
		
	return public_method;

	

});